﻿/**
 * 本地存储
 * @namespace Store
 * @name core.Store
 * @static
 * @see https://github.com/cloudhead/store.js
 */
core.Store = (function () {
  var api = {},
    win = window,
    doc = win.document,
    localStorageName = 'localStorage',
    globalStorageName = 'globalStorage',
    storage;
  /**
   * 设置DATA内容
   * @name core.Store.set
   * @function
   * @grammar core.Store.set(key, value)
   * @param {String} key 要写入的键名
   * @param {String} value 要写入的键值
   * @example
   * core.Store.set('username', 'marcus');
   * core.Store.set('user', { name: 'marcus', likes: 'javascript' })
   */
  api.set = function (key, value) {
  };
  /**
   * 读取DATA内容
   * @name core.Store.get
   * @function
   * @grammar core.Store.get(key)
   * @param {String} key 要读取的键名
   * @example core.Store.get('username');
   */
  api.get = function (key) {
  };
  /**
   * 删除DATA内容
   * @name core.Store.remove
   * @function
   * @grammar core.Store.remove(key)
   * @param {String} key 要删除的键名
   * @example core.Store.remove('username');
   */
  api.remove = function (key) {
  };
  /**
   * 清空DATA内容
   * @name core.Store.clear
   * @function
   * @grammar core.Store.clear()
   * @example core.Store.clear();
   */
  api.clear = function () {
  };
  if (localStorageName in win && win[localStorageName]) {
    storage = win[localStorageName];
    api.set = function (key, val) {
      storage.setItem(key, val)
    };
    api.get = function (key) {
      return storage.getItem(key)
    };
    api.remove = function (key) {
      storage.removeItem(key)
    };
    api.clear = function () {
      storage.clear()
    };
  } else if (globalStorageName in win && win[globalStorageName]) {
    storage = win[globalStorageName][win.location.hostname];
    api.set = function (key, val) {
      storage[key] = val
    };
    api.get = function (key) {
      return storage[key] && storage[key].value
    };
    api.remove = function (key) {
      delete storage[key]
    };
    api.clear = function () {
      for (var key in storage) {
        delete storage[key]
      }
    };
  } else if (doc.documentElement.addBehavior) {
    function getStorage() {
      if (storage) {
        return storage
      }
      storage = doc.body.appendChild(doc.createElement('div'));
      storage.style.display = 'none';
      // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
      // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
      storage.addBehavior('#default#userData');
      storage.load(localStorageName);
      return storage;
    }

    api.set = function (key, val) {
      var storage = getStorage();
      storage.setAttribute(key, val);
      storage.save(localStorageName);
    };
    api.get = function (key) {
      var storage = getStorage();
      return storage.getAttribute(key);
    };
    api.remove = function (key) {
      var storage = getStorage();
      storage.removeAttribute(key);
      storage.save(localStorageName);
    };
    api.clear = function () {
      var storage = getStorage();
      var attributes = storage.XMLDocument.documentElement.attributes;
      ;
      storage.load(localStorageName);
      for (var i = 0, attr; attr = attributes[i]; i++) {
        storage.removeAttribute(attr.name);
      }
      storage.save(localStorageName);
    }
  }
  return api;
})();
